/*
 * frmSubjectSchedule.java
 *
 * Created on May 3, 2010, 8:39 AM
 */

package GUI;

import Entity.SubjectSchedule;
import Validate_Connect.GUIInteraction;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author  Shinichi
 */
public class frmSubjectSchedule extends javax.swing.JInternalFrame {

    /** Creates new form frmSubjectSchedule */
    public frmSubjectSchedule() {
        initComponents();
        refreshForm();
        GUIInteraction.readToCombo("select * from tblSchedule", _cboScheduleID, "ScheduleID");
        GUIInteraction.readToCombo("select * from tblSubjects", _cboSubjectID, "SubID");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        _tblSchedule = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return false;   //Disallow the editing of any cell
            }
        };
        _tblSchedule.getTableHeader().setReorderingAllowed(false);
        jScrollPane1 = new javax.swing.JScrollPane();
        _tblSubject = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return false;   //Disallow the editing of any cell
            }
        };
        _tblSubject.getTableHeader().setReorderingAllowed(false);
        jPanel1 = new javax.swing.JPanel();
        _cboScheduleID = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        _cboSubjectID = new javax.swing.JComboBox();
        _btndelete = new javax.swing.JButton();
        _btnaddNew = new javax.swing.JButton();
        _btnreset = new javax.swing.JButton();
        _btnupdate = new javax.swing.JButton();
        _btnclose = new javax.swing.JButton();
        _btnprint = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        _tblSubjectSchedule = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return false;   //Disallow the editing of any cell
            }
        };
        _tblSubjectSchedule.getTableHeader().setReorderingAllowed(false);
        jPanel4 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setTitle("AITSM - Subject Schedule");

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));

        _tblSchedule.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        _tblSchedule.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                _tblScheduleMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(_tblSchedule);

        _tblSubject.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        _tblSubject.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                _tblSubjectMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(_tblSubject);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Information"));

        jLabel1.setText("Schedule ID:");

        jLabel2.setText("Subject ID:");

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(33, 33, 33)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel1)
                    .add(jLabel2))
                .add(21, 21, 21)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(_cboSubjectID, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(_cboScheduleID, 0, 173, Short.MAX_VALUE))
                .addContainerGap(58, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(_cboScheduleID, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(_cboSubjectID, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        _btndelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Delete2.png"))); // NOI18N
        _btndelete.setText("Delete");
        _btndelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _btndeleteActionPerformed(evt);
            }
        });

        _btnaddNew.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/list_add2.png"))); // NOI18N
        _btnaddNew.setText("Add New");
        _btnaddNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _btnaddNewActionPerformed(evt);
            }
        });

        _btnreset.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/reset2.png"))); // NOI18N
        _btnreset.setText("Reset");
        _btnreset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _btnresetActionPerformed(evt);
            }
        });

        _btnupdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/update2.png"))); // NOI18N
        _btnupdate.setText("Update");
        _btnupdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _btnupdateActionPerformed(evt);
            }
        });

        _btnclose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Logout16x16.png"))); // NOI18N
        _btnclose.setText("Close");
        _btnclose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _btncloseActionPerformed(evt);
            }
        });

        _btnprint.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/printer2.png"))); // NOI18N
        _btnprint.setText("Print");
        _btnprint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _btnprintActionPerformed(evt);
            }
        });

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("List"));

        _tblSubjectSchedule.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        _tblSubjectSchedule.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                _tblSubjectScheduleMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(_tblSubjectSchedule);

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 293, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 132, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        jPanel4.setBackground(new java.awt.Color(0, 153, 102));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Subject Schedule");

        org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel4Layout.createSequentialGroup()
                .add(269, 269, 269)
                .add(jLabel3)
                .addContainerGap(294, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel3)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .add(22, 22, 22)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 328, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
                .add(27, 27, 27))
            .add(jPanel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(jPanel3Layout.createSequentialGroup()
                .add(37, 37, 37)
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel3Layout.createSequentialGroup()
                        .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, _btndelete, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, _btnaddNew))
                        .add(30, 30, 30)
                        .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(_btnreset, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 102, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(_btnupdate, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 102, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(27, 27, 27)
                        .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(_btnclose, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 104, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(_btnprint, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 104, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(37, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel3Layout.createSequentialGroup()
                .add(jPanel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel3Layout.createSequentialGroup()
                        .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(_btnaddNew)
                            .add(_btnreset)
                            .add(_btnprint))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(_btndelete)
                            .add(_btnupdate)
                            .add(_btnclose)))
                    .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(101, 101, 101))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 402, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void _btnaddNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__btnaddNewActionPerformed
    int _ScheduleID = Integer.valueOf(_cboScheduleID.getSelectedItem().toString());
    String _SubjectID = _cboSubjectID.getSelectedItem().toString();
    Entity.SubjectSchedule _SubSchedule = new SubjectSchedule(_ScheduleID, _SubjectID);
    Interact.SubjectSchedules.insertSubjectSchedule(_SubSchedule);    
    resetForm();
    refreshForm();
}//GEN-LAST:event__btnaddNewActionPerformed

private void _btnresetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__btnresetActionPerformed
    resetForm();
}//GEN-LAST:event__btnresetActionPerformed

private void _btnprintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__btnprintActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event__btnprintActionPerformed

private void _btncloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__btncloseActionPerformed
    int i = JOptionPane.showConfirmDialog(null, "Do you want to exit?", "Exit", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
    if (i == 0) {
        this.dispose();
    }
}//GEN-LAST:event__btncloseActionPerformed

private void _btnupdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__btnupdateActionPerformed
    int _ScheduleID = Integer.valueOf(_cboScheduleID.getSelectedItem().toString());
    String _SubjectID = _cboSubjectID.getSelectedItem().toString();
    Entity.SubjectSchedule _SubSchedule = new SubjectSchedule(_ScheduleID, _SubjectID);
    Interact.SubjectSchedules.updateSubjectSchedule(_SubSchedule);    
    resetForm();
    refreshForm();
}//GEN-LAST:event__btnupdateActionPerformed

private void _btndeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__btndeleteActionPerformed
    int _ScheduleID = Integer.valueOf(_cboScheduleID.getSelectedItem().toString());
    Interact.SubjectSchedules.deleteSubjectSchedule(_ScheduleID);
    resetForm();
    refreshForm();
}//GEN-LAST:event__btndeleteActionPerformed

private void _tblSubjectMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event__tblSubjectMouseClicked
    int i = _tblSubject.getSelectedRow();
    _cboSubjectID.setSelectedItem(_tblSubject.getValueAt(i, 0));
}//GEN-LAST:event__tblSubjectMouseClicked

private void _tblScheduleMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event__tblScheduleMouseClicked
    int i = _tblSchedule.getSelectedRow();
    _cboScheduleID.setSelectedItem(_tblSchedule.getValueAt(i, 0));
}//GEN-LAST:event__tblScheduleMouseClicked

private void _tblSubjectScheduleMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event__tblSubjectScheduleMouseClicked
    int i = _tblSubjectSchedule.getSelectedRow();
    _cboScheduleID.setSelectedItem(_tblSubjectSchedule.getValueAt(i, 0));
    _cboSubjectID.setSelectedItem(_tblSubjectSchedule.getValueAt(i, 1));
}//GEN-LAST:event__tblSubjectScheduleMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton _btnaddNew;
    private javax.swing.JButton _btnclose;
    private javax.swing.JButton _btndelete;
    private javax.swing.JButton _btnprint;
    private javax.swing.JButton _btnreset;
    private javax.swing.JButton _btnupdate;
    private javax.swing.JComboBox _cboScheduleID;
    private javax.swing.JComboBox _cboSubjectID;
    private javax.swing.JTable _tblSchedule;
    private javax.swing.JTable _tblSubject;
    private javax.swing.JTable _tblSubjectSchedule;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    // End of variables declaration//GEN-END:variables

    public void refreshForm()
    {
        try {
            GUIInteraction.readToTable("select SubID, SubName from tblSubjects", _tblSubject);
            GUIInteraction.readToTable("select s.ScheduleID, u.FullName, convert(char(12),s.StartDate,101) as StartDate, convert(char(12),s.EndDate,101) as EndDate from tblSchedule s, tblUser u where s.UserID = u.UserID", _tblSchedule);
            GUIInteraction.readToTable("select * from tblSubjectSchedule", _tblSubjectSchedule);
        } catch (SQLException ex) {
            Logger.getLogger(frmSubjectSchedule.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void resetForm(){
        _cboScheduleID.setSelectedIndex(0);
        _cboSubjectID.setSelectedIndex(0);
    }
}
